package com.gzx.plugin.tjzy.modular.trace.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzx.plugin.tjzy.modular.trace.bo.GzxTracePlanAddBo;
import com.gzx.plugin.tjzy.modular.trace.bo.GzxTracePlanQueryBo;
import com.gzx.plugin.tjzy.modular.trace.constant.TransPlanConstant;
import com.gzx.plugin.tjzy.modular.trace.entity.GzxTracePlan;
import com.gzx.plugin.tjzy.modular.trace.mapper.GzxTracePlanMapper;
import com.gzx.plugin.tjzy.modular.trace.service.GzxTracePlanService;
import com.gzx.plugin.tjzy.modular.trace.util.TransPlanDateTimeUtil;
import com.gzx.plugin.tjzy.modular.trace.vo.GzxTracePlanVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.sses.consts.SseMsgTypeConstant;
import vip.xiaonuo.common.sses.entity.SseMessage;
import vip.xiaonuo.common.sses.util.CommonSseUtil;
import vip.xiaonuo.common.util.MapstructUtils;

import java.util.List;

/**
 * 跟进通知计划 gzx_trace_plan
 *
 * @author xbl
 * &#064;date  2025-03-28
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class GzxTracePlanServiceImpl implements GzxTracePlanService {

    private final GzxTracePlanMapper baseMapper;

    private final CommonSseUtil sseUtil;

    /**
     * 跟进计划通知
     */
    @DS("sms")
    @Override
    public Page<GzxTracePlanVo> page(GzxTracePlanQueryBo tracePlanQueryBo) {
        return baseMapper.selectVoPage(new Page<>(tracePlanQueryBo.getCurrent(), tracePlanQueryBo.getSize()),
                Wrappers.<GzxTracePlan>lambdaQuery()
                        .eq(StringUtils.isNotBlank(tracePlanQueryBo.getTraceName()), GzxTracePlan::getTraceName, tracePlanQueryBo.getTraceName())
                        .eq(StringUtils.isNotBlank(tracePlanQueryBo.getCreateUser()), GzxTracePlan::getCreateUser, tracePlanQueryBo.getCreateUser())
                        .eq(tracePlanQueryBo.getTraceUser() != 0, GzxTracePlan::getTraceUser, tracePlanQueryBo.getTraceUser())
                        .eq(tracePlanQueryBo.getCaseId() != 0, GzxTracePlan::getCaseId, tracePlanQueryBo.getCaseId())
                        .ge(StringUtils.isNotBlank(tracePlanQueryBo.getBeginTime()), GzxTracePlan::getNextTraceTime, tracePlanQueryBo.getBeginTime())
                        .le(StringUtils.isNotBlank(tracePlanQueryBo.getEndTime()), GzxTracePlan::getNextTraceTime, tracePlanQueryBo.getEndTime())
                        .orderByDesc(GzxTracePlan::getNextTraceTime)
        );
    }

    /**
     * 跟进计划通知定时任务
     */
    @DS("sms")
    @Override
    public void noticeCase() {
        //读取到所有待通知的数据
        List<GzxTracePlan> gzxTracePlans = baseMapper.selectList(Wrappers.<GzxTracePlan>lambdaQuery()
                .eq(GzxTracePlan::getNoticeStatus, TransPlanConstant.NOTICE_STATUS)
                //从现在起两分钟内的
                .ge(GzxTracePlan::getNextTraceTime, TransPlanDateTimeUtil.getCurrentDateTime())
                .le(GzxTracePlan::getNextTraceTime, TransPlanDateTimeUtil.getDateTimeAfterTwoMinutes())
                .orderByAsc(GzxTracePlan::getNextTraceTime)
        );

        log.info("跟进计划通知定时任务扫描到的数据条数 {} ", gzxTracePlans.size());

        gzxTracePlans.forEach(item -> {
            String content = JSON.toJSONString(item);
            //通知sse
            log.info("通知sse的数据 {} ", content);
            sseUtil.sendMessageToUser(item.getTraceUser().toString(), new SseMessage(SseMsgTypeConstant.TRANCE_PLAN, content));
            //通知成功的要把状态改成1
            item.setNoticeStatus(TransPlanConstant.NOTICED_STATUS);
            baseMapper.updateById(item);
        });
    }

    @Override
    public void add(GzxTracePlanAddBo tracePlanAddBo) {
        baseMapper.insert(MapstructUtils.convert(tracePlanAddBo, GzxTracePlan.class));
    }

    @Override
    public void edit(GzxTracePlanAddBo tracePlanAddBo) {
        baseMapper.updateById(MapstructUtils.convert(tracePlanAddBo, GzxTracePlan.class));
    }

    @Override
    public void del(Long id) {
        baseMapper.deleteById(id);
    }
}
